<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="广告状态" prop="stat">
        <el-select v-model="queryParams.stat" placeholder="请选择广告状态">
          <el-option v-for="(item, index) in field101Options" :key="index" :label="item.label"
                     :value="item.value" :disabled="item.disabled"></el-option>
        </el-select>
      </el-form-item>

      <el-form-item label="广告分类" prop="type">
        <el-select v-model="queryParams.type" placeholder="请选择广告分类" clearable>
          <el-option
            v-for="dict in dict.type.advise_type"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="星级" prop="stars">
        <el-select v-model="queryParams.stars" placeholder="请选择星级" clearable>
          <el-option
            v-for="dict in dict.type.sys_stars"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="名称" prop="name">
        <el-input
          v-model="queryParams.name"
          placeholder="请输入名称"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
       <el-form-item label="所在镇街道" prop="town">
              <el-select v-model="queryParams.town" placeholder="请选择所在镇街道" clearable>
                <el-option
                  v-for="dict in dict.type.subdistrict"
                  :key="dict.value"
                  :label="dict.label"
                  :value="dict.value"
                />
              </el-select>
            </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['system:advertise:add']"
        >新增
        </el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="success"
          plain
          icon="el-icon-edit"
          size="mini"
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['system:advertise:edit']"
        >修改
        </el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['system:advertise:remove']"
        >删除
        </el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
          @click="handleExport"
          v-hasPermi="['system:advertise:export']"
        >导出
        </el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="advertiseList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center"/>
      <el-table-column label="序号" align="center" type="index"/>
      <el-table-column label="广告分类" align="center" prop="type">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.advise_type" :value="scope.row.type"/>
        </template>
      </el-table-column>
      <el-table-column label="星级" align="center" prop="stars">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.sys_stars" :value="scope.row.stars"/>
        </template>
      </el-table-column>
      <el-table-column label="名称" align="center" prop="name"/>
      <el-table-column label="排序" align="center" prop="priority"/>
      <el-table-column label="数量" align="center" prop="num"/>
      <el-table-column label="单价" align="center" prop="price"/>
      <el-table-column label="图片" align="center" prop="img" width="100">
        <template slot-scope="scope">
          <image-preview :src="scope.row.img" :width="50" :height="50"/>
        </template>
      </el-table-column>

      <el-table-column label="地址" align="center" prop="address"/>
      <el-table-column label="尺寸" align="center" prop="size"/>

      <el-table-column label="上下架" align="center" key="stat">
        <template slot-scope="scope">
          <el-switch
            v-model="scope.row.stat"
            active-value="0"
            inactive-value="1"
            @change="handleStatusChange(scope.row)"
          ></el-switch>
        </template>
      </el-table-column>

      <el-table-column label="状态" align="center" prop="statName"/>
       <el-table-column label="所在镇街道" align="center" prop="town">
              <template slot-scope="scope">
                <dict-tag :options="dict.type.subdistrict" :value="scope.row.town"/>
              </template>
            </el-table-column>
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['system:advertise:edit']"
          >修改
          </el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['system:advertise:remove']"
          >删除
          </el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改广告管理对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="500px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="广告分类" prop="type">
          <el-select v-model="form.type" placeholder="请选择广告分类">
            <el-option
              v-for="dict in dict.type.advise_type"
              :key="dict.value"
              :label="dict.label"
              :value="dict.value"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="星级" prop="stars">
          <el-select v-model="form.stars" placeholder="请选择星级">
            <el-option
              v-for="dict in dict.type.sys_stars"
              :key="dict.value"
              :label="dict.label"
              :value="dict.value"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="名称" prop="name">
          <el-input v-model="form.name" placeholder="请输入名称"/>
        </el-form-item>
        <el-form-item label="单价" prop="price">
          <el-input v-model="form.price" placeholder="请输入单价"/>
        </el-form-item>
        <el-form-item label="排序" prop="price">
          <el-input-number :disabled="specsVolDisabled" v-model="form.priority" placeholder="请输入排序"/>
        </el-form-item>
        <el-form-item label="数量" prop="price">
          <el-input-number :disabled="specsVolDisabled" v-model="form.num" placeholder="请输入数量"/>
        </el-form-item>
        <el-form-item label="详情">
          <editor v-model="form.detail" :min-height="192"/>
        </el-form-item>
        <el-form-item label="地址" prop="address">
          <el-autocomplete :popper-append-to-body="false" value-key="title" v-model="form.address" :fetch-suggestions="searchAddress"
                           placeholder="请输入地址"
                           :trigger-on-focus="false"
                       ></el-autocomplete>
        </el-form-item>

        <el-form-item label="经度" prop="lng">
          <el-input v-model="form.lng" placeholder="请输入经度" />
        </el-form-item>
        <el-form-item label="纬度" prop="lat" >
          <el-input v-model="form.lat" placeholder="请输入纬度" />
        </el-form-item>

        <el-form-item label="图片" prop="img">
          <image-upload :limit="1" v-model="form.img"/>
        </el-form-item>

        <el-form-item label="尺寸" prop="size">
          <el-input v-model="form.size" placeholder="请输入尺寸"/>
        </el-form-item>

        <el-form-item label="所在镇街道" prop="town">
          <el-select v-model="form.town" placeholder="请选择所在镇街道">
            <el-option
              v-for="dict in dict.type.subdistrict"
              :key="dict.value"
              :label="dict.label"
              :value="parseInt(dict.value)"
            ></el-option>
          </el-select>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import {
  listAdvertise,
  changeUserStatus,
  getAdvertise,
  delAdvertise,
  addAdvertise,
  updateAdvertise,
  searchByPOI
} from "@/api/system/advertise";

export default {
  name: "Advertise",
  dicts: ['advise_type', 'sys_stars','subdistrict'],
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      specsVolDisabled: false,
      // 总条数
      total: 0,
      // 广告管理表格数据
      advertiseList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,

      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        type: null,
        stars: null,
        name: null,
        town: null
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {},
      field101Options: [{
        "label": "下架",
        "value": 1
      }, {
        "label": "出售中",
        "value": 0
      }, {
        "label": "已出售",
        "value": 3
      }],
    };
  },
  created() {
    this.getList();
  },
  methods: {
    /** 查询广告管理列表 */
    getList() {
      this.loading = true;
      listAdvertise(this.queryParams).then(response => {
        this.advertiseList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 用户状态修改
    handleStatusChange(row) {
      let text = row.stat === "0" ? "上架" : "下架";
      this.$modal.confirm('确认要"' + text + '""' + row.name + '"广告吗？').then(function () {
        return changeUserStatus(row.id, row.stat);
      }).then(() => {
        this.$modal.msgSuccess(text + "成功");
      }).catch(function () {
        row.stat = row.stat === "0" ? "1" : "0";
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        type: null,
        stars: null,
        name: null,
        price: null,
        detail: null,
        lat: null,
        lng: null,
        createTime: null,
        img: null,
        address: null,
        size: null,
        tieredPrice: null,
        town: null
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.id)
      this.single = selection.length !== 1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加广告管理";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const id = row.id || this.ids
      getAdvertise(id).then(response => {
        this.form = response.data;
        this.open = true;
        this.title = "修改广告管理";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.id != null) {
            updateAdvertise(this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addAdvertise(this.form).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids;
      this.$modal.confirm('是否确认删除当前数据？').then(function () {
        return delAdvertise(ids);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => {
      });
    },
 /*  handleSelect(e) {
      console.log(e)
      const {location}  =e
      this.form.lat = location.lat
      this.form.lng = location.lng
    },*/
    searchAddress(keyword, cb) {
      if (keyword && keyword !== '') {
        searchByPOI(keyword).then((e) => {
          if (e && e.status == 0) {
            const list = e.data
            cb(list)
          } else {
            cb([])
          }
        })
      }
    },

    /** 导出按钮操作 */
    handleExport() {
      this.download('system/advertise/export', {
        ...this.queryParams
      }, `advertise_${new Date().getTime()}.xlsx`)
    }
  }
};
</script>
